package com.leoman.service.impl;

import com.leoman.core.Constant;
import com.leoman.dao.UserDao;
import com.leoman.entity.User;
import com.leoman.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Created by Administrator on 2016/3/8.
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;

    @Override
    public List<User> findAll() {
        return userDao.findAll();
    }

    @Override
    public Page<User> find(Integer pageNum, Integer pageSize) {
        return userDao.findAll(new PageRequest(pageNum - 1, pageSize));
    }

    @Override
    public Page<User> find(Integer pageNum) {
        return userDao.findAll(new PageRequest(pageNum - 1, Constant.PAGE_DEF_SIZE));
    }

    @Override
    public User getById(Integer id) {
        return userDao.findOne(id);
    }

    @Override
    public User deleteById(Integer id) {
        User _user = userDao.findOne(id);
        userDao.delete(_user);
        return null;
    }

    @Override
    public User create(User User) {
        return userDao.save(User);
    }

    @Override
    public User update(User User) {
        return userDao.save(User);
    }

    @Override
    @Transactional
    public void deleteAll(Integer[] ids) {
        for (Integer id : ids) {
            deleteById(id);
        }
    }

    @Override
    public User findOneByParams(String account, String password) {
        return userDao.findOneByParams(account, password);
    }

    @Override
    public User findOneByAccount(String account) {
        return userDao.findOneByAccount(account);
    }
}
